import moment from "moment";
export default {
  dateFormat: function(timestamp, pattern = "YYYY-MM-DD HH:mm:ss") {
    if (!timestamp) return '';
    timestamp = timestamp.toString().length === 10 ? Number(timestamp) * 1000 : timestamp
    return moment(timestamp).format(pattern);
  },
  // 浮点去尾
  float2int: function(num) {
    num = num.toString();
    let index = num.indexOf(".");
    if (index !== -1) {
      num = num.substring(0, index + 1);
    } else {
      num = num.substring(0);
    }
    return parseInt(num);
  },
  // 时间差标签 秒
  dateDiff: function(dateTimeStamp) {
    let minute = 1000 * 60;
    let hour = minute * 60;
    let day = hour * 24;
    let halfamonth = day * 15;
    let month = day * 30;
    let now = new Date().getTime();
    let diffValue = now - (dateTimeStamp * 1000);
    if (diffValue < 0) {
      return;
    }
    let monthC = diffValue / month;
    let weekC = diffValue / (7 * day);
    let dayC = diffValue / day;
    let hourC = diffValue / hour;
    let minC = diffValue / minute;
    let result = dateTimeStamp;
    if (monthC >= 1) {
      result = "" + parseInt(monthC) + "月前";
    } else if (weekC >= 1) {
      result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
      result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
      result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
      result = "" + parseInt(minC) + "分钟前";
    } else
      result = "刚刚";
    return result;
  },
  // 时间转字符串(订单剩余时间) 秒 时分秒 分秒
  time2str: function(time, index = 0) {
    let d = Math.floor(time / (3600 * 24));
    time = time - (d * 3600 * 24);
    let h = Math.floor(time / 3600);
    time = time - (h * 3600)
    let m = Math.floor(time / 60);
    let s = time - (m * 60);
    if (index) {
      return [d, h, m, s][index - 1];
    }
    return [d, h, m, s];
  }
};
